<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="700px"
  >
    <el-form
      ref="form"
      :model="form"
      label-width="120px"
      :rules="rules"
      :disabled="dialogObj.read"
      v-if="dialogObj.show"
      status-icon
    >
      <el-form-item label="名称：" prop="name">
        <el-input
          v-model="form.name"
          style="width:200px;"
          size="mini"
          maxlength="50"
          :disabled="dialogObj.id != ''"
          :placeholder="placeholderTips.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="电话：" prop="phone">
        <el-input
          v-model="form.phone"
          style="width:200px;"
          size="mini"
          maxlength="30"
          :placeholder="placeholderTips.phone"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input
          v-model="form.email"
          style="width:200px;"
          size="mini"
          maxlength="50"
          placeholder="请输入邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input
          type="textarea"
          v-model="form.remark"
          maxlength="100"
          style="width:400px;"
          size="mini"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="sub" size="mini">确 定</el-button>
      <el-button @click="dialogObj.show = false" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { _debounce } from "@u/debounce";
import {
  placeholderTips,
  validatePhoneNumber,
  validateName,
  validateEmail
} from "@u/validate";
import { AddAgentApi, updateAgentApi } from "@s/api";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  data() {
    return {
      agentStatus: true,
      placeholderTips: placeholderTips,
      form: {
        id: "",
        name: "",
        phone: "",
        email: "",
        remark: ""
      },
      rules: {
        name: [
          { required: true, trigger: "blur", message: "请输入代理商名称" },
          { validator: validateName }
        ],
        email: [{ validator: validateEmail }],
        phone: [
          { required: true, trigger: "blur", message: "请输入手机号" },
          { validator: validatePhoneNumber }
        ]
      }
    };
  },
  methods: {
    initDialog() {
      if (this.dialogObj.id) {
        this.form.id = this.dialogObj.id;
        this.form.name = this.dialogObj.form.name;
        this.form.phone = this.dialogObj.form.phone;
        this.form.email = this.dialogObj.form.email;
        this.form.remark = this.dialogObj.form.remark;
      } else {
        this.form.name = "";
        this.form.phone = "";
        this.form.email = "";
        this.form.remark = "";
      }
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogObj.id) {
            this.updateSub();
          } else {
            this.addSub();
          }
        }
      });
    }),
    updateSub() {
      let json = JSON.parse(JSON.stringify(this.form));
      updateAgentApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "修改成功！"
        });
      });
    },
    addSub() {
      let json = JSON.parse(JSON.stringify(this.form));
      AddAgentApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "添加成功！"
        });
      });
    }
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.initDialog();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
